package com.briup.jd2511.jd2511_product_source.controller;

import com.briup.jd2511.jd2511_product_source.exception.ServiceException;
import com.briup.jd2511.jd2511_product_source.pojo.ManagerAnimal;
import com.briup.jd2511.jd2511_product_source.pojo.ext.ManagerAnimalExt;
import com.briup.jd2511.jd2511_product_source.result.Result;
import com.briup.jd2511.jd2511_product_source.service.ManagerAnimalService;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "动物管理模块")
@RestController
@RequestMapping("/animal")
public class AnimalController {
    @Autowired
    private ManagerAnimalService managerAnimalService;

    @GetMapping
    @Operation(summary = "分页+条件查询动物")
    @Parameters(value = {
            @Parameter(name = "aHealthy",description = "动物健康状态"),
            @Parameter(name = "aGender",description = "动物性别"),
            @Parameter(name = "pageName",description = "页码"),
            @Parameter(name = "pageSize",description = "每页数量")
    })
    public Result queryAllAnimalByPage(@RequestParam(required = false)String aHealthy,
                                       @RequestParam(required = false)String aGender,
                                       @RequestParam(defaultValue = "1")Integer pageName,
                                       @RequestParam(defaultValue = "10")Integer pageSize){
        PageInfo<ManagerAnimalExt> pageInfo = managerAnimalService.queryAllAnimalByPage(aHealthy,aGender,pageName,pageSize);
        return Result.success(pageInfo);
    }

    @PostMapping("/saveOrUpdate")
    @Operation(summary = "新增/修改动物信息")
    public Result saveOrUpdate(@RequestBody ManagerAnimal managerAnimal) {
        try {
            managerAnimalService.saveOrUpdate(managerAnimal);
            return Result.success();
        } catch (ServiceException e) {
            return Result.failure(e.getResultCode());
        }
    }

}
